<?php
namespace app\index\controller\Project;

use app\index\controller\Base;

class planinfo extends base_project
{
    public $tableName = "ProjectPlaninfo";
    public function index()
    {
        $paln_id=input("planid");
        $map["project_id"]=$this->base_project_id;
        $map["plan_id"]=intval($paln_id);
        $this->page($map,10,$this->tableName,"id desc","project,plan,user");
        $code_status = model("CodeMap")->get_value("plan_info_status"); 
        $data["code_status"] = array_column($code_status, "m_name", "m_value");
        $data["plan_id"]=$paln_id;
        $this->assign($data);
        return view();
    }

    /**
     *  添加
     */
    public function add()
    {
        $model = model('ProjectPlaninfo');
        if (request()->instance()->isPost()) {
            $post = request()->instance()->param();        
            $data["plan_id"] = $post["plan_id"];
            $data["project_id"] =$this->base_project_id;
            $data["title"] = $post["title"];
            $data["info"] = $post["info"];
            $data["uid"] =$this->member_id;
            $data["status"] = 1;   

            $ProjectPlaninfoModel = model("ProjectPlaninfo");
            $result = $ProjectPlaninfoModel->save($data);
            if (empty($result)) {
                $this->error("操作失败");

            }
            $this->success("操作成功", url("/Index/Project/Planinfo/index",["planid"=> $post["plan_id"]]));
        }
        $paln_id=input("plan_id");
        $data["plan_id"]=$paln_id;
        $this->assign($data);
        return view();
    }

    /**
     *  修改
     */
    public function edit()
    {
        $ProjectPlaninfoModel = model('ProjectPlaninfo');
        if (request()->instance()->isPost()) {
            $post = request()->instance()->param();        
            $data["title"] = $post["title"];
            $data["info"] = $post["info"];       
            $data["status"] = $post["status"];
      

            $map["id"] = $post["id"];
            $result = $ProjectPlaninfoModel->save($data, $map);
            if (empty($result)) {
                $this->error("操作失败");

            }
            $this->success("操作成功",  url("/Index/Project/Planinfo/index",["planid"=> $post["plan_id"]]));
        }

        $id = input("id");

        $data["info"] = $ProjectPlaninfoModel->get($id);

        $this->assign($data);
        return view();
    }

    public function del()
    {
        $id = input("id");
        if (empty($id)) {
            $this->error("删除失败");
        }
        $ProjectPlaninfoModel = model('ProjectPlaninfo');
        $result = $ProjectPlaninfoModel->where('id', intval($id))->delete();
        if ($result) {
            $this->success("删除成功");
        }
        $this->error("删除失败");
    }

    public  function status(){
        if (request()->instance()->isPost()) {
            $post = request()->instance()->param();
            $data["status"]=$post["status"];
           $model= model("ProjectPlaninfo");
            $result =   $model->where(["id"=>$post["id"]])->update($data);
            if ($result) {
                $this->success("操作成功");
            }
            $this->error("操作失败");

        }

        $plan_id=input("id");
        $code_status = model("CodeMap")->get_value("project_plan_status"); 
        $project_id=$this->base_project_id;
        $info=  model("ProjectPlaninfo")->where(["id"=>$plan_id,"project_id"=> $project_id])->with("project")->find();
        $data["info"]=$info;
        $data["code_status"]=$code_status;
        $this->assign($data);
        return view();
    }


    public  function done()
    {
        $id = input("id");
        if (empty($id)) {
            $this->error("操作失败");
        }
        $ProjectPlanModel = model('ProjectPlaninfo');
        $project_id=$this->base_project_id;
        $data["status"]=3;     
        
        $result=model("ProjectPlaninfo")->where(["id"=>$id,"project_id"=>$project_id])->update($data);;
        if ($result) {
            $this->success("操作成功");
        }
        $this->error("操作失败");
    }

}
